<?php
/**
 * 
 * @author Radovan Dvorsky
 * @version 20101212
 */
class HarmonogramService extends ServiceAbstract{
	
	private $_mapper;
	
	public function __construct(){
		$this->_mapper = new Db_Mapper_Harmonogram();
	}
	
	/**
	 * Odstrani studenta z hramonogramu
	 * 
	 * @param integer $hid
	 * @param integer $IDst
	 */
	public function deleteStudentByHidIDst($hid,$IDst){
				
		$result = $this->_mapper->getTable()->getAdapter()->query(
						'DELETE FROM harmonogram_student WHERE hid = ? AND ID_st = ?',
						array((int)$hid,(int)$IDst)
		);	
		
		
		return ($result->rowCount() > 0) ? true : false;
	}
	
	/**
	 * Prida studenta studenta do hramonogramu
	 * 
	 * @param integer $hid ID harmonogramu
	 * @param integer $IDst ID studenta
	 * 
	 * @return boolean
	 */
	public function addStudent($hid,$IDst){
		
		$data = array(
			'hid' => (int)$hid,
			'ID_st' => (int)$IDst,
		);		
		
		$mapper = new Db_Mapper_HarmonogramStudent();
		$result = $mapper->save(new Db_Model_HarmonogramStudent($data),true);
		
		return ($result > 0) ? true : false;
	}
	
	/**
	 * NEFUNGUJE
	 * 
	 * @deprecated NEFUNGUJE
	 */
	public function addGroupToHarmonogram(){
		
		$hid = (int)$this->getParam('hid');
		$students = $this->getParam('students');
		$sql = "INSERT INTO harmonogram_student(hid,ID_st) VALUES ";
		
		$students = explode(",",rtrim($students,','));
		
		
		foreach ($students	as $student) {
			if((int)$student == 0){
				echo 'false';
				return;
			}
			$sql .= "(" . (int)$hid . "," . (int)$student ."),";
		}	
		
		echo $sql = rtrim($sql,",");
		$result = $this->_mapper->getTable()->getAdapter()->query($sql);
				
		echo ($result->rowCount() > 0) ? 'true' : 'false';
		
	}
	
	
	/**
	 * Vytovri novy harmonogramy
	 * 
	 * @param string $name
	 * @param integer $cid
	 * @param string $date
	 * @param integer $idOboru
	 * 
	 * @return integer id vlozeneho riadku
	 */
	public function add($name,$cid,$date,$idOboru){
		
		$data = array(
			'harmonogram_name' => $name,
			'cid' => (int)$cid,
			'date' => $data,
			'Id_oboru' => (int)$idOboru
		);
		
		return (int)$this->_mapper->save(new Db_Model_Harmonogram($data));
	}
	
	/**
	 * Odstrani cely harmonogram vratene v nom priradenych studentov
	 * 
	 * @param integer $hid
	 * 
	 * @return boolean
	 */
	public function remove($hid){		
		
		$result = $this->_mapper->delete(new Db_Model_Harmonogram(array('hid' => (int)$hid)));
		
		return ($result > 0) ? true : false;
	}
	
	/**
	 * Vráti všetky harmonogramy
	 * 
	 * @return array
	 */
	public function fetchAll(){
		
		$id_oboru = array();
		$harmonograms = array();
		
		$select = $this->_mapper->getTable()->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
		$select->setIntegrityCheck(false)
		->join('commission','commission.cid=harmonogram.cid',array('name'))		
		->order('harmonogram.harmonogram_name');
		
		
		$harmonograms = $this->_mapper->getTable()->fetchAll($select)->toArray();

		foreach($harmonograms as $harmonogram){
			$id_oboru[] = $harmonogram['Id_oboru'];
		}
		
		$mapper = new Db_Mapper_StObory();
		$selectObory = $mapper->getTable()->select()->where('Id_oboru IN(?)',$id_oboru,Zend_Db::INT_TYPE);
		$oboryResults = $mapper->fetchAll($selectObory);
		
		for ($i = 0; $i < count($harmonograms); $i++) {
			for ($j = 0; $j < count($oboryResults); $j++) {
				if($oboryResults[$j]['Id_oboru'] == $harmonograms[$i]['Id_oboru']){
					$harmonograms[$i]['Obor'] = $oboryResults[$j]['Obor'];
				}
			}
		}
		
		return $harmonograms;
		
	}
	
	/**
	 * Vrati vsetky harmonogrami podla hid
	 * 
	 * @param int $hid id harmonogramu
	 * 
	 * @return array|false
	 */
	public function fetchHarmonogram($hid){		
		
		$harmonograms = array();
		
		$select = $this->_mapper->getTable()->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
		$select->setIntegrityCheck(false)
		->join('harmonogram_student','harmonogram.hid=harmonogram_student.hid')
		->where('harmonogram.hid = ?',$hid);
	
		/*
		 * Vsetky harmonogrami vratane studentov k danemu harmonogramu
		 */
		$harmonograms = $this->_mapper->getTable()->fetchAll($select)->toArray();
		
		if(count($harmonograms) == 0){
			return array();
		}
		
		/*
		 * Prepojenie so skolskou databazou; Prida mena studentov a zkratku tried
		 */
		$ID_st = array ();
		
		for($i = 0; $i < count ( $harmonograms ); $i ++) {
			$ID_st [$i] = $harmonograms [$i]['ID_st'];
		}
		
		$idStudentov = rtrim ( implode ( ",", $ID_st ), ',' );
		
		$studentDbTable = new Db_DbTable_Student ();
		$adapter = $studentDbTable->getAdapter ();
		$query = $adapter->query ( "SELECT Student.ID_st,Student.Jmeno, Student.Prijmeni,T_Tridy.Zkratka,T_Tridy.ID_tr FROM Student
							 JOIN Student_has_Tridy ON Student_has_Tridy.Student_ID_st=Student.ID_st
							 JOIN T_Tridy ON T_Tridy.ID_tr=Student_has_Tridy.Tridy_ID_tr
							 WHERE Student.ID_st IN($idStudentov)
							 AND T_Tridy.Zkratka LIKE '%3%'
							 ORDER BY Student.Prijmeni" );
		
		$students = $query->fetchAll ();
			
		
		for($i = 0; $i < count ( $harmonograms ); $i ++) {
			for($j = 0; $j < count ( $students ); $j ++) {
				if ($harmonograms [$i] ['ID_st'] == $students [$j] ['ID_st']) {					
					$harmonograms [$i] ['Jmeno'] = $students [$j] ['Jmeno'];
					$harmonograms [$i] ['Prijmeni'] = $students [$j] ['Prijmeni'];
					$harmonograms [$i] ['ID_tr'] = $students [$j] ['ID_tr'];
					$harmonograms [$i] ['Zkratka'] = $students [$j] ['Zkratka'];
				}
			}
		}

		return (count($harmonograms) > 0) ? $harmonograms : false;		
	}
}
?>